package com.my.mytest.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.my.mytest.R;
import com.my.mytest.bean.ImageData;
import com.my.mytest.util.GlideRoundTransform;
import com.my.mytest.util.LogUtil;
import com.my.mytest.util.MViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Android Studio.
 * User: mwb
 * Date: 2020/12/8 0008
 * Time: 上午 11:42
 * Describe:
 */
public class LandRAnimAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<ImageData> data;

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    public LandRAnimAdapter(Context context) {
        this.context = context;
        this.data = new ArrayList<>();
    }

    public void setData(final List<ImageData> data, final RecyclerView recyclerView, final int flag) {
        this.data = data;
        for (int i = 0; i < data.size(); i++) {
            final int finalI = i;
            Glide.with(context)
                    .asBitmap()
                    .load(data.get(i).getImageRouse())
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(new CustomTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            resource.getWidth();
                            data.get(finalI).setImgHeight(resource.getHeight());
                            data.get(finalI).setImgWidth(resource.getWidth());
                            LogUtil.i("height:" + resource.getHeight());
                            runLayoutAnim(recyclerView, flag);
                            notifyDataSetChanged();
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                        }
                    });
        }
    }

    @Override
    public int getItemViewType(int position) { // TODO MWB
        if (data.get(position).isHeader()) {
            return TYPE_HEADER;
        } else {
            return TYPE_ITEM;
        }
    }

    private void runLayoutAnim(RecyclerView recyclerView, int flag) {
        final LayoutAnimationController controller;
        if (flag == 1) { // 由上到下
            controller =
                    AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_fall_down);
        } else if (flag == 2) { // 从左往右
            controller =
                    AnimationUtils.loadLayoutAnimation(context, R.anim.layout_anim_left2right);
        } else {
            return;
        }

        notifyDataSetChanged();

        recyclerView.setLayoutAnimation(controller);
        recyclerView.getAdapter().notifyDataSetChanged();
        recyclerView.scheduleLayoutAnimation();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_HEADER:
                view = LayoutInflater.from(context).inflate(R.layout.item_header, parent, false);
                return new HeaderHolder(view);

            case TYPE_ITEM:
                view = LayoutInflater.from(context).inflate(R.layout.item_card_view, parent, false);
                return new ItemHolder(view);

        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case TYPE_HEADER:
                if (holder instanceof HeaderHolder) {
                    StaggeredGridLayoutManager.LayoutParams clp = (StaggeredGridLayoutManager.LayoutParams) ((HeaderHolder) holder).rl_header.getLayoutParams();
                    clp.setFullSpan(true);
                    ViewGroup.LayoutParams lp = ((HeaderHolder) holder).rl_header.getLayoutParams();
                    lp.width = context.getResources().getDisplayMetrics().widthPixels;
                    lp.height = MViewUtil.dp2px(context, 200);

                    ((HeaderHolder) holder).rl_header.setLayoutParams(lp);
                }

                break;
            case TYPE_ITEM:
                if (holder instanceof ItemHolder) {
                    final ViewGroup.LayoutParams lp1 = ((ItemHolder) holder).iv.getLayoutParams();

                    lp1.width = context.getResources().getDisplayMetrics().widthPixels / 2;
//                    lp1.height = (int) data.get(position).getImgHeight();
                    lp1.height = (int) (context.getResources().getDisplayMetrics().widthPixels / 2 * (data.get(position).getImgHeight() / data.get(position).getImgWidth()));
                    ((ItemHolder) holder).iv.setLayoutParams(lp1);

                    ((ItemHolder) holder).tv_position.setText("第" + position + "个");

                    Glide.with(context)
                            .load(data.get(position).getImageRouse())
                            .transform(new CenterCrop(), new GlideRoundTransform(context, 10))
                            .into(((ItemHolder) holder).iv);
                }
                break;
        }
    }


    @Override
    public int getItemCount() {
        return data.size();
    }

    private class ItemHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_header;
        ImageView iv;
        TextView tv_position;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);

            iv = itemView.findViewById(R.id.item_card_iv);
            rl_header = itemView.findViewById(R.id.rl_header);
            tv_position = itemView.findViewById(R.id.tv_position);
        }
    }

    private class HeaderHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_header;

        public HeaderHolder(@NonNull View itemView) {
            super(itemView);

            rl_header = itemView.findViewById(R.id.rl_header);
        }
    }
}
